import Home from "@/views/05_views_guard_路由守卫/Home.vue";
import About from "@/views/05_views_guard_路由守卫/About.vue";

import {createRouter , createWebHistory} from 'vue-router'
import Bar from "@/views/05_views_guard_路由守卫/Bar.vue";
import Foo from "@/views/05_views_guard_路由守卫/Foo.vue";

//1：定义路由
const routes = [
    {
        path: '/',
        component: Home,
        meta:{auth:true},
    },
    {
        path: '/about',
        component: About,
        meta:{auth:true},
        children: [
            {
                path: 'bar',
                name: 'bar',
                component: Bar,
                meta:{auth:true},
                //单组件跳转权限控制
                // beforeEnter(to,from,next){
                //     if(to.meta.auth){
                //         next('/');
                //     }else{
                //         next();
                //     }
                // }
            },
            {
                path: 'foo',
                name: 'foo',
                component: Foo,
                meta:{auth:false}
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


router.beforeEach((to,from,next)=>{
    if(to.meta.auth){
        next();
    }else{
        next('/');
    }
    //console.log(to,from);
    //next()
})

//2：导出
export default router;
